package com.example.lista_zadan;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.*;

import java.util.Map;

public class Main extends Activity {
    private static final int NO_TASK_TAME_DIALOG = 1;
    private static final int CONFIRM_REMOVE_TASK = 2;
    public static final String PREFS = "preferences";

    ListView list;

    Button buttonAdd;

    EditText editTextTaskName;

    SpecialAdapter adapter;

    TextView textView;

    Integer taskPosition;

    SharedPreferences settings;

    TextView howMayTasks;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        list = (ListView) findViewById(R.id.list);
        buttonAdd = (Button) findViewById(R.id.buttonAdd);
        editTextTaskName = (EditText) findViewById(R.id.editTextTaskName);
        textView = (TextView)findViewById(R.id.taskInfo);
        howMayTasks = (TextView) findViewById(R.id.textViewHowMayTasks);

        adapter = new SpecialAdapter(this);

        LoadPrefs();
        refreshListView();

        buttonAdd.setOnClickListener(new SluchaczPrzyciskuPlus());

        list.setOnItemLongClickListener(new SluchaczListy());
    }

    class SluchaczPrzyciskuPlus implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            if(editTextTaskName.getText().toString().equals(""))
                showDialog(NO_TASK_TAME_DIALOG);
            else {
                SavePrefs(editTextTaskName.getText().toString(), editTextTaskName.getText().toString());
                adapter.addItem(editTextTaskName.getText().toString());
                list.setAdapter(adapter);
                editTextTaskName.setText("");
                refreshListView();
            }
        }
    }

    class SluchaczListy implements AdapterView.OnItemLongClickListener {
        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                                       int pos, long id) {
            taskPosition = pos;
            showDialog(CONFIRM_REMOVE_TASK);

            return true;
        }
    }

    private void refreshListView() {
            if (list.getCount() == 0) {
                textView.setText("Brak zadań do wykonania");
                howMayTasks.setText("");
            }
            else {
                textView.setText("");
                howMayTasks.setText("Zadań do wykonania: " + list.getCount());
            }
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch(id)
        {
            case NO_TASK_TAME_DIALOG:
            {
                return new AlertDialog.Builder(this)
                        .setMessage("Nie wpisałeś nazwy zadania")
                        .setPositiveButton("OK", new DialogInterface.OnClickListener()
                        {
                            @Override
                            public void onClick(DialogInterface arg0, int arg1) {

                            }
                        }).create();
            }
            case CONFIRM_REMOVE_TASK:
            {
                return new AlertDialog.Builder(this)
                        .setMessage("Czy chcesz usunąć to zadanie?")
                        .setPositiveButton("Tak", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface arg0, int arg1) {
                                settings = getSharedPreferences(PREFS, Context.MODE_PRIVATE);
                                String taskName = adapter.getItemString(taskPosition);
                                PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().remove(taskName).commit();
                                adapter.removeItem(taskPosition);
                                list.setAdapter(adapter);
                                refreshListView();
                            }
                        }).setNegativeButton("Nie" , new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        }).create();
            }
        }
        return null;
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private void SavePrefs(String key, String value) {
        settings = getSharedPreferences(PREFS, Context.MODE_PRIVATE);
        PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putString(
                key, value).commit();
    }

    private void LoadPrefs() {
        settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        for (Map.Entry<String, ?> e : settings.getAll().entrySet()) {
            adapter.addItem((String)e.getValue());
        }
        list.setAdapter(adapter);
    }
}
